package main

import (
	"fmt"
	"net"
	"time"
)

func main() {
	packetconn, err := net.ListenPacket("udp", "127.0.0.1:9999")
	if err != nil {
		fmt.Println(err)
		return
	}

	// 处理客户端
	for {
		ctx := make([]byte, 1024)
		n, addr, err := packetconn.ReadFrom(ctx)
		if err != nil {
			fmt.Println(err)
			continue
		}
		fmt.Printf("客户端[%s]发送数据:%s", addr, string(ctx[:n]))
		_, err = packetconn.WriteTo([]byte(time.Now().Format("2006/01/02 15:04:05")), addr)
		if err != nil {
			fmt.Println(err)
			continue
		}
	}
	packetconn.Close()
}
